package com.dy.dynotinstalled.room;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

/**
 * @author Li Xuyang
 * @date : 2019/9/26 19:32
 */
public class NotDoorInformationRepository {
    private LiveData<List<NotDoorInformation>> allDoorInformationLive;

    private NotDoorInformationDao notDoorInformationDao;




    NotDoorInformationRepository(Context context) {
        InformationDatabase informationDatabase = InformationDatabase.getDatabase(context.getApplicationContext());
        notDoorInformationDao = informationDatabase.doorInformationDao();
        allDoorInformationLive = notDoorInformationDao.getAllDoorInformationLive();

    }

    void insertDoorInformation(NotDoorInformation... notDoorInformation) {
        new InsertAsyncTask(notDoorInformationDao).execute(notDoorInformation);
    }

    void updateDoorInformation(NotDoorInformation... notDoorInformation) {
        new UpdateAsyncTask(notDoorInformationDao).execute(notDoorInformation);
    }

    void deleteDoorInformation(NotDoorInformation... notDoorInformation) {
        new DeleteAsyncTask(notDoorInformationDao).execute(notDoorInformation);
    }

    void deleteAllDoorInformation() {
        new DeleteAllAsyncTask(notDoorInformationDao).execute();
    }


    LiveData<List<NotDoorInformation>> getAllDoorInformationLive() {
        return allDoorInformationLive;
    }



    static class InsertAsyncTask extends AsyncTask<NotDoorInformation, Void, Void> {
        private NotDoorInformationDao notDoorInformationDao;

        InsertAsyncTask(NotDoorInformationDao notDoorInformationDao) {
            this.notDoorInformationDao = notDoorInformationDao;
        }

        @Override
        protected Void doInBackground(NotDoorInformation... notDoorInformation) {
            notDoorInformationDao.insertDoorInformation(notDoorInformation);
            return null;
        }

    }

    static class UpdateAsyncTask extends AsyncTask<NotDoorInformation, Void, Void> {
        private NotDoorInformationDao notDoorInformationDao;

        UpdateAsyncTask(NotDoorInformationDao notDoorInformationDao) {
            this.notDoorInformationDao = notDoorInformationDao;
        }

        @Override
        protected Void doInBackground(NotDoorInformation... notDoorInformation) {
            notDoorInformationDao.updateDoorInformation(notDoorInformation);
            return null;
        }

    }

    static class DeleteAsyncTask extends AsyncTask<NotDoorInformation, Void, Void> {
        private NotDoorInformationDao notDoorInformationDao;

        DeleteAsyncTask(NotDoorInformationDao notDoorInformationDao) {
            this.notDoorInformationDao = notDoorInformationDao;
        }

        @Override
        protected Void doInBackground(NotDoorInformation... notDoorInformation) {
            notDoorInformationDao.deleteDoorInformation(notDoorInformation);
            return null;
        }

    }

    static class DeleteAllAsyncTask extends AsyncTask<Void, Void, Void> {
        private NotDoorInformationDao notDoorInformationDao;

        DeleteAllAsyncTask(NotDoorInformationDao notDoorInformationDao) {
            this.notDoorInformationDao = notDoorInformationDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            notDoorInformationDao.deleteAllDoorInformation();
            return null;
        }

    }
}
